package com.cy.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.cy.mapper.Nmapper;
import com.cy.pojo.JsonResult;
import com.cy.pojo.Nuser;
import com.cy.pojo.UserVO;
import com.cy.service.Nservice;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/user")
@Api(value = "新用户管理类",tags = "新用户接口管理")
public class NewController {

    @Autowired
    private Nservice nservice;

    @ApiOperation("根据id查询用户")
    @ApiImplicitParam(name = "id", value = "用户id", dataType = "int", paramType = "path", required = true)
    @GetMapping("/getUser/{id}")
    public JsonResult findUser(@PathVariable("id") Integer id){
        return new JsonResult(nservice.findById(id));
    }

    @ApiOperation("查询所有用户")
    @ApiImplicitParam(paramType = "Nuser", name = "User-findAll", value = "用户", required = true)
    @GetMapping("/findObjects")
    public JsonResult findObjects(){
        return new JsonResult(nservice.findObjects());
    }


    @ApiOperation("根据姓名查询用户")
    @ApiImplicitParam(paramType = "Nuser", name = "User-search", value = "用户", required = true)
    @GetMapping("/findUserByName/{name}")
    public JsonResult findUserByName(@PathVariable(value = "name")  String name){
        Nuser nuser=nservice.findByName(name);
        return new JsonResult(nuser);
    }

    @ApiOperation("根据姓名嵌套查询用户")
    @ApiImplicitParam(paramType = "Nuser", name = "User-searchColl", value = "用户", required = true)
    @GetMapping("/findUserByNameColl/{name}")
    public JsonResult findUserByNameColl( @PathVariable(value = "name")  String name){
        return new JsonResult(nservice.findByNameColl(name));
    }

    @ApiOperation("添加新用户")
    /**   @ApiImplicitParam(paramType = "Nuser", name = "User-add", value = "用户", required = true)
     *
    */
    @PostMapping(value = "/saveUser",produces = {"application/json;charset=UTF-8"})
    public JsonResult saveUser( @RequestBody UserVO userVO){
        nservice.saveUser(userVO);
        return new JsonResult("save Ok");
    }

    @ApiOperation("修改用户")
    @ApiImplicitParam(paramType = "Nuser", name = "User-update", value = "用户", required = true)
    @PutMapping("/updateUser")
    public JsonResult updateUser(@Valid @RequestBody Nuser nuser){
        nservice.updateUser(nuser);
        return new JsonResult("update Ok");
    }

    @ApiOperation("删除用户")
    @ApiImplicitParam(paramType = "Nuser", name = "User-delete", value = "用户", required = true)
    @DeleteMapping("/deleteUserById/{id}")
    public JsonResult deleteUserById(@Valid @PathVariable Integer id){
        nservice.deleteUser(id);
        return new JsonResult("delete Ok");
    }

    @ApiOperation("批量修改用户")
    @ApiImplicitParam(paramType = "Nuser", name = "User-updateBatch", value = "用户", required = true)
    @PutMapping("/updateBatch")
    public JsonResult updateBatchByIds(@Valid List <Integer>ids){
        nservice.updateBatch(ids);
        return new JsonResult("update Ok");
    }

    @ApiOperation("批量删除用户")
    @ApiImplicitParam(paramType = "Nuser", name = "User-deleteBatch", value = "用户", required = true)
    @DeleteMapping("/deleteBatch")
    public JsonResult deleteBatchByIds(@Valid List <Integer>ids){
        nservice.deleteBatch(ids);
        return new JsonResult("delete Ok");
    }

    @ApiOperation("批量添加用户")
    @ApiImplicitParam(paramType = "Nuser", name = "User-saveBatch", value = "用户", required = true)
    @PutMapping("/saveBatch")
    public JsonResult saveBatch(@Valid  List<Nuser> nuserList){
        nservice.saveBatchNuser(nuserList);
        return new JsonResult("save Ok");
    }

    @ApiOperation("批量查询用户")
    @ApiImplicitParam(paramType = "Nuser", name = "User-findBatch", value = "用户", required = true)
    @GetMapping("/findBatch/ids")
    public JsonResult findBatch(@Valid List <Integer>ids){
        nservice.findBatchNuser(ids);
        return new JsonResult("find Ok");
    }

    @ApiOperation("用户查询分页")
    @PostMapping("/findUserPageObjects")
    public JsonResult findUserPage(@RequestBody Map<String,Object> map){
       IPage<Nuser>page= nservice.selectUserPage(map);
        return new JsonResult(page);
    }

    @ApiOperation("用户查询分页")
    @GetMapping("/findUserPage/{pageCurrent}/{pageSize}")
    public JsonResult findUserPage2(@PathVariable int pageCurrent,
                                    @PathVariable int pageSize){

        return new JsonResult(nservice.selectPageObj(pageCurrent, pageSize));
    }

   
 }
